#include <system/window_system.h>
#include <common_header.h>
#include <GLFW/glfw3.h>
#include <system/input_mapping.h>

namespace primal
{

	WindowSystem::WindowSystem()
	{
		glfwInit();
		glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
	}

	WindowSystem::~WindowSystem()
	{
		glfwTerminate();
	}

	void GLFW_KEY_PROCESS(GLFWwindow* window, int key, int scancode, int action, int mods)
	{
		input::set_input(glfw_input_map[key], { action });
	}

	void GLFW_MOUSE_MOVE_PROCESS(GLFWwindow* window, double xpos, double ypos)
	{
		input::set_input(input::mouse_position, { (float)xpos,(float)ypos });
	}

	void GLFW_MOUSE_BUTTON_PROCESS(GLFWwindow* window, int button, int action, int mods)
	{

	}

	void GLFW_WINDOW_RESIZE_PROCESS(GLFWwindow* window, int width, int height)
	{
		input::set_input(input::window_resize, { width, height });
	}

	Window WindowSystem::CreateWnd(const char* title, s32 width, s32 height, bool visibility)
	{
		glfwWindowHint(GLFW_VISIBLE, visibility ? GLFW_TRUE : GLFW_FALSE);
		auto temp_window{ glfwCreateWindow(width,height,title,nullptr,nullptr) };
		glfwSetWindowSizeCallback(temp_window, GLFW_WINDOW_RESIZE_PROCESS);
		glfwSetCursorPosCallback(temp_window, GLFW_MOUSE_MOVE_PROCESS);
		glfwSetKeyCallback(temp_window, GLFW_KEY_PROCESS);
		return Window{ temp_window };
	}

	void WINDOW_DELETER::operator()(GLFWwindow* p) const noexcept
	{
		glfwDestroyWindow(p);
	}

	WindowSystem& GetWS()
	{
		static WindowSystem ws{};
		return ws;
	}
}